package com.ming.mallchat.common.user.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.mallchat.common.common.domain.enums.YesOrNoEnum;
import com.ming.mallchat.common.user.mapper.UserBackpackMapper;
import com.ming.mallchat.common.user.pojo.UserBackpack;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserBackPackDao extends ServiceImpl<UserBackpackMapper, UserBackpack> {

    /**
     * 获取用户改名卡次数
     * @param uid 用户id
     * @param itemId 物品id
     * @return {@link Integer}
     */
    public Integer getCountValidItemId(Long uid, Long itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .count();
    }

    /**
     * @param uid    用户id
     * @param itemId 物品id
     * @return {@link UserBackpack}
     */
    public UserBackpack getFirstValidItem(Long uid, Long itemId) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, itemId)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .orderByAsc(UserBackpack::getId)
                .last("limit 1")
                .one();
    }

    /**
     * @param modifyNameItem 用户所拥有的改名卡信息
     * @return {@link Boolean}
     */
    public Boolean userItem(UserBackpack modifyNameItem) {
        return lambdaUpdate()
                .eq(UserBackpack::getId, modifyNameItem.getId())
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .set(UserBackpack::getStatus, YesOrNoEnum.YES.getStatus())
                .update();
    }

    public List<UserBackpack> getByItemIds(Long uid, List<Long> itemIds) {
        return lambdaQuery()
                .eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .in(UserBackpack::getItemId, itemIds)
                .list();
    }

    public UserBackpack getIdempotent(String idempotent) {
        return lambdaQuery()
                .eq(UserBackpack::getIdempotent, idempotent)
                .one();
    }

    public List<UserBackpack> getByItemIds(List<Long> uidList, List<Long> backpacks) {
        return lambdaQuery().in(UserBackpack::getUid, uidList)
                .in(UserBackpack::getItemId, backpacks)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .list();
    }
}
